// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.

let ui = {};
let $ = require("jquery");

ui.init = function(){
  ui.cm = CodeMirror(
    document.body, {
      value:"function name(params) {}",
      lineNumbers: true,
      mode:"javascript",
      keyMap: "vim",
      theme: "dracula",
      extraKeys: {"Ctrl-Space": "autocomplete"},
    }
  );

  ui.cm.on("keyup", function(cm, event) {
    var ExcludedIntelliSenseTriggerKeys =
    {
    "8": "backspace",
    "9": "tab",
    "13": "enter",
    "16": "shift",
    "17": "ctrl",
    "18": "alt",
    "19": "pause",
    "20": "capslock",
    "27": "escape",
    "33": "pageup",
    "34": "pagedown",
    "35": "end",
    "36": "home",
    "37": "left",
    "38": "up",
    "39": "right",
    "40": "down",
    "45": "insert",
    "46": "delete",
    "49": "!",
    "91": "left window key",
    "92": "right window key",
    "93": "select",
    "107": "add",
    "109": "subtract",
    "110": "decimal point",
    "111": "divide",
    "112": "f1",
    "113": "f2",
    "114": "f3",
    "115": "f4",
    "116": "f5",
    "117": "f6",
    "118": "f7",
    "119": "f8",
    "120": "f9",
    "121": "f10",
    "122": "f11",
    "123": "f12",
    "144": "numlock",
    "145": "scrolllock",
    "186": "semicolon",
    "187": "equalsign",
    "188": "comma",
    "189": "dash",
    "190": "period",
    "191": "slash",
    "192": "graveaccent",
    "220": "backslash",
    "222": "quote"};
    var __Cursor = ui.cm.getDoc().getCursor();
    var __Token = ui.cm.getTokenAt(__Cursor);
    if (!cm.state.completionActive && !ExcludedIntelliSenseTriggerKeys[(event.keyCode || event.which).toString()] &&  __Token.type && __Token.string != '' &&  __Token.string != ' ' && __Token.string != '\n')
    {
      console.log(__Token, event);
      CodeMirror.commands.autocomplete(cm, null, { completeSingle: false });
    }});

  var body = $("body");
  body.css("margin", "0px");
  var body_top_margin = body.css("margin-top");
  var body_bottom_margin = body.css("margin-bottom");
  body.height(window.screen.availHeight - parseInt(body_top_margin) - parseInt(body_bottom_margin));
  var cm = $(".CodeMirror");
  cm.height(body.height());
  cm.css("margin:", "20px");
  console.log(body_top_margin, body_bottom_margin, $("body").height());
};

ui.init();
